import Qs from 'qs'

function toString(data) {
  let transData = data
  if (transData === null) {
    transData = ''
  } else if (transData instanceof Array) {
    for (let i = 0; i < transData.length; i++) {
      transData[i] = toString(transData[i])
    }
  } else if (typeof transData === 'object') {
    for (let k in transData) {
      transData[k] = toString(transData[k])
    }
  }
  return transData
}

export default {
  url: null,
  // baseURL: '/api',
  method: 'POST',
  transformRequest: [
    function (data) {
      let ret = ''
      for (let it in data) {
        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
      }
      return ret
      // 把对象转json发送给后台
      // data = JSON.stringify(data)
      // return data
    }
  ],
  transformResponse: [
    function (data) {
      let dataTrans = data
      // 兼容ie浏览器
      // if (typeof dataTrans === 'string') {
      //   dataTrans = JSON.parse(dataTrans)
      // }
      // dataTrans = toString(dataTrans)
      return dataTrans
    }
  ],
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
  },
  params: {},
  paramsSerializer: function (params) {
    return Qs.stringify(params)
  },
  data: {},
  // timeout: 30000,

  withCredentials: false,
  responseType: 'json'
}
